/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rmiinterface;

import com.thoughtworks.xstream.XStream;
import java.awt.List;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author chris
 */
public class RMIServer extends UnicastRemoteObject implements RMIInterface {

    private XStream xstream = new XStream();
    private LinkedList<Person> list;
    private ObjectOutputStream output;

    //default constructor
    public RMIServer() throws RemoteException, IOException {
        Person p1 = new Person("Ivan", "ger", true, true);
        Person p2 = new Person("Petar", "bng", true, true);
        Person p3 = new Person("Milen", "kyj", false, true);
        list = new LinkedList<>();
        list.add(p1);
        list.add(p2);
        list.add(p3);

        xstream.alias("person", Person.class);

        output = new ObjectOutputStream(new FileOutputStream("test.xml"));
        for (int i = 0; i < list.size(); i++) {
            output.writeBytes(xstream.toXML(list.get(i)));
        }
        output.close();
    }

    // get function for the linked list
    public LinkedList<Person> getList() {
        return list;
    }

    //function to encrypt a number
    @Override
    public String encrypt(String num) {
        char[] a = num.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) ((int) a[i] + 5);
            if ((int) a[i] > 57) {
                a[i] = (char) ((a[i] - 57) + 47);
            }
        }
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < a.length; i++) {
            str.append(a[i]);
        }
        return str.toString();
    }

    // function to decrypt a number
    @Override
    public String decrypt(String num) {
        char[] a = num.toCharArray();
        for (int i = 0; i < a.length; i++) {
            if ((int) (a[i] - 5) < 48) {
                a[i] = (char) ((int) a[i] + 5);
            } else {
                a[i] = (char) ((int) a[i] - 5);
            }

        }
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < a.length; i++) {
            str.append(a[i]);
        }
        return str.toString();
    }
}
